#include "dexiosfmdirview.h"

#include <QContextMenuEvent>
#include <QDebug>
#include <QFileSystemModel>
#include <QHeaderView>
#include <QMenu>
#include <QSortFilterProxyModel>

DexiosFMDirView::DexiosFMDirView(QWidget * parent)
	: QTreeView(parent)
	{
		setItemsExpandable(false);
		setSortingEnabled(true);
		header()->setResizeMode(QHeaderView::ResizeToContents);
	}

void DexiosFMDirView::contextMenuEvent(QContextMenuEvent * e)
	{
		QModelIndex index = indexAt(e->pos());
		QMenu menu(this);

		if (QSortFilterProxyModel * proxy_model = qobject_cast<QSortFilterProxyModel *>(model()))
		{
			index = proxy_model->mapToSource(index);

			if (QFileSystemModel * mdl = qobject_cast<QFileSystemModel *>(proxy_model->sourceModel()))
			{
				if (mdl->isDir(index))
				{
					// Use the action from the main UI instead
//					QAction * add_to_favorites = menu.addAction(tr("Add to favorites"));
//
//					connect(add_to_favorites, SIGNAL(triggered()),
//							this, SIGNAL(addToFavorites()));

					QAction * open_action = menu.addAction(tr("Open"));

					connect(open_action, SIGNAL(triggered()),
							this, SIGNAL(openPath()));
				}

				else if (mdl->type(index).contains(QRegExp("jpe?g|png|gif|bmp|xbm|xpm|pbm|pgm|ppm|tiff", Qt::CaseInsensitive)))
				{
					QAction * preview_action = menu.addAction(tr("Preview Image"));

					connect(preview_action, SIGNAL(triggered()),
							this, SIGNAL(previewImage()));
				}
			}
		}

		if (menu.actions().count())
			menu.exec(e->globalPos());
	}
